{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3b9d5fe6-8023-4635-bae5-41f98a6be4fa",
   "metadata": {},
   "source": [
    "### 练习题目：通过 API 获取天气预报数据\n",
    "\n",
    "#### 目的：\n",
    "\n",
    "本次练习的目标是帮助学生熟悉如何使用 Python 中的 `requests` 库与 API 进行交互，学习解析 JSON 格式的数据，并将提取出的信息以结构化的方式展示给用户。\n",
    "\n",
    "#### 要求：\n",
    "\n",
    "1. 编写一个 Python 脚本，通过访问天气预报 API 来获取指定城市的天气数据。学生可以使用 [中国天气网](https://www.weather.com.cn/weather1d/101180101.shtml) 提供的不同城市代码来查询对应城市的天气情况。\n",
    "\n",
    "2. 代码中需要完成以下功能：\n",
    "   - 向 `http://t.weather.itboy.net/api/weather/city/{city_code}` 发送 GET 请求，其中 `{city_code}` 为城市代码。\n",
    "   - 提取并展示以下信息：\n",
    "     - 当前城市及其天气更新时间。\n",
    "     - 当前的温度、湿度、空气质量、感冒提醒等。\n",
    "     - 未来几天的天气预报，包括日期、天气类型（如晴、阴、雨等）、温度范围、风向、空气质量指数、以及其他天气提醒。\n",
    "     - 昨天的天气情况，类似未来几天的预报内容。\n",
    "\n",
    "3. 程序中需要处理网络请求失败或无法获取天气数据的情况，给用户输出友好的提示信息。\n",
    "\n",
    "4. 学生可以从 [中国天气网](https://www.weather.com.cn/weather1d/101180101.shtml) 中选择不同城市的代码，替换示例代码中的 `city_code` 参数，查看其他城市的天气情况。常见城市的代码可以通过查看网页地址中的数字获取，如：\n",
    "   - 北京：`101010100`\n",
    "   - 上海：`101020100`\n",
    "   - 广州：`101280101`\n",
    "   - 郑州：`101180101`\n",
    "\n",
    "#### 代码示例：\n",
    "\n",
    "可以参考如下 Python 代码示例，并根据需求进行修改："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27e5fa30-d183-49f3-9fc2-bbfb70ddc166",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9fcd13e3-1a30-4bb3-8e7d-9e53defac6cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "天气预报城市：郑州市\n",
      "更新时间：21:14\n",
      "当前温度：19.5℃，湿度：51%，空气质量：良。极少数敏感人群应减少户外活动\n",
      "\n",
      "未来几天的天气预报：\n",
      "23日(星期一), 2024-09-23\n",
      "    多云, 低温 13℃到高温 24℃, 风向：东南风 2级, 空气质量指数：41\n",
      "    阴晴之间，谨防紫外线侵扰\n",
      "\n",
      "24日(星期二), 2024-09-24\n",
      "    晴, 低温 13℃到高温 26℃, 风向：南风 2级, 空气质量指数：64\n",
      "    愿你拥有比阳光明媚的心情\n",
      "\n",
      "25日(星期三), 2024-09-25\n",
      "    晴, 低温 16℃到高温 28℃, 风向：东南风 2级, 空气质量指数：76\n",
      "    愿你拥有比阳光明媚的心情\n",
      "\n",
      "26日(星期四), 2024-09-26\n",
      "    阴, 低温 18℃到高温 26℃, 风向：东风 2级, 空气质量指数：87\n",
      "    不要被阴云遮挡住好心情\n",
      "\n",
      "27日(星期五), 2024-09-27\n",
      "    多云, 低温 17℃到高温 28℃, 风向：东南风 2级, 空气质量指数：83\n",
      "    阴晴之间，谨防紫外线侵扰\n",
      "\n",
      "28日(星期六), 2024-09-28\n",
      "    阴, 低温 19℃到高温 27℃, 风向：东南风 3级, 空气质量指数：76\n",
      "    不要被阴云遮挡住好心情\n",
      "\n",
      "29日(星期日), 2024-09-29\n",
      "    大雨, 低温 18℃到高温 28℃, 风向：西北风 3级, 空气质量指数：78\n",
      "    出门最好穿雨衣，勿挡视线\n",
      "\n",
      "30日(星期一), 2024-09-30\n",
      "    阴, 低温 13℃到高温 18℃, 风向：东北风 3级, 空气质量指数：78\n",
      "    不要被阴云遮挡住好心情\n",
      "\n",
      "01日(星期二), 2024-10-01\n",
      "    阴, 低温 12℃到高温 15℃, 风向：东风 1级, 空气质量指数：86\n",
      "    不要被阴云遮挡住好心情\n",
      "\n",
      "02日(星期三), 2024-10-02\n",
      "    多云, 低温 12℃到高温 22℃, 风向：南风 2级, 空气质量指数：94\n",
      "    阴晴之间，谨防紫外线侵扰\n",
      "\n",
      "03日(星期四), 2024-10-03\n",
      "    晴, 低温 16℃到高温 27℃, 风向：西南风 1级, 空气质量指数：92\n",
      "    愿你拥有比阳光明媚的心情\n",
      "\n",
      "04日(星期五), 2024-10-04\n",
      "    阴, 低温 17℃到高温 25℃, 风向：东南风 2级, 空气质量指数：76\n",
      "    不要被阴云遮挡住好心情\n",
      "\n",
      "05日(星期六), 2024-10-05\n",
      "    阴, 低温 19℃到高温 25℃, 风向：东南风 2级, 空气质量指数：80\n",
      "    不要被阴云遮挡住好心情\n",
      "\n",
      "06日(星期日), 2024-10-06\n",
      "    中雨, 低温 16℃到高温 19℃, 风向：西北风 2级, 空气质量指数：82\n",
      "    记得随身携带雨伞哦\n",
      "\n",
      "07日(星期一), 2024-10-07\n",
      "    阴, 低温 14℃到高温 20℃, 风向：东北风 2级, 空气质量指数：32\n",
      "    不要被阴云遮挡住好心情\n",
      "\n",
      "昨日天气：\n",
      "22日(星期日), 2024-09-22\n",
      "    晴, 低温 14℃到高温 24℃, 风向：东北风 2级, 空气质量指数：28\n",
      "    愿你拥有比阳光明媚的心情\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "def fetch_weather_data(city_code):\n",
    "    url = f\"http://t.weather.itboy.net/api/weather/city/{city_code}\"\n",
    "    try:\n",
    "        response = requests.get(url)\n",
    "        if response.status_code != 200:\n",
    "            raise Exception(f\"HTTP error! status: {response.status_code}\")\n",
    "        return response.json()\n",
    "    except Exception as error:\n",
    "        print('Could not fetch weather data:', error)\n",
    "        return None\n",
    "\n",
    "def print_weather_forecast(city_code):\n",
    "    weather_data = fetch_weather_data(city_code)\n",
    "    if not weather_data:\n",
    "        print(\"No weather data available.\")\n",
    "        return\n",
    "\n",
    "    city_info = weather_data['cityInfo']\n",
    "    city = city_info['city']\n",
    "    print(f\"天气预报城市：{city}\\n更新时间：{city_info['updateTime']}\")\n",
    "\n",
    "    data = weather_data['data']\n",
    "    print(f\"当前温度：{data['wendu']}℃，湿度：{data['shidu']}，空气质量：{data['quality']}。{data['ganmao']}\\n\")\n",
    "\n",
    "    forecast = data['forecast']\n",
    "    print(\"未来几天的天气预报：\")\n",
    "    for day in forecast:\n",
    "        print(f\"{day['date']}日({day['week']}), {day['ymd']}\")\n",
    "        print(f\"    {day['type']}, {day['low']}到{day['high']}, 风向：{day['fx']} {day['fl']}, 空气质量指数：{day['aqi']}\")\n",
    "        print(f\"    {day['notice']}\\n\")\n",
    "\n",
    "    yesterday = data['yesterday']\n",
    "    print(\"昨日天气：\")\n",
    "    print(f\"{yesterday['date']}日({yesterday['week']}), {yesterday['ymd']}\")\n",
    "    print(f\"    {yesterday['type']}, {yesterday['low']}到{yesterday['high']}, 风向：{yesterday['fx']} {yesterday['fl']}, 空气质量指数：{yesterday['aqi']}\")\n",
    "    print(f\"    {yesterday['notice']}\\n\")\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    city_code = '101180101'  # 示例：郑州市的城市代码\n",
    "    print_weather_forecast(city_code)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b431ba2a-0b88-424a-a426-2ab78d15247e",
   "metadata": {},
   "source": [
    "https://www.juhe.cn/ 一个api集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7df53d45-c493-4e3f-8bc9-fbf2f393f7b7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
